/**  
 * @Title: DebitCardSvcImpl.java
 * @Package com.atm.service.impl
 * @author daluosi
 * @date 2018年3月14日
 * @version V1.0  
 */
package com.atm.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.atm.constants.Constants;
import com.atm.entity.Bill;
import com.atm.entity.Card;
import com.atm.service.DebitCardSvc;
import com.atm.util.DataOperateUtil;
import com.atm.util.DateFormatUtil;

/**
 * @ClassName: DebitCardSvcImpl
 * @Description: 借记卡服务实现类
 * @author 姜向阳
 * @date 2018年3月14日
 * @since JDK 1.8
 */
public class DebitCardSvcImpl extends CardSvcImpl implements DebitCardSvc{


	/* (非 Javadoc)
	 * 
	 * 
	 * @param cardID
	 * @param money
	 * @return
	 * @see com.atm.service.CardSvc#getMoney(java.lang.String, java.lang.String)
	 */
	@Override
	public Map<String, Object> getMoney(String cardID, String money) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();

		DataOperateUtil dataOperateUtil = DataOperateUtil.getDataOperateUtil();
		Card card = dataOperateUtil.readCardData(cardID);
		if (card == null) {
			resultMap.put(Constants.RESULT_KEY_CODE, Constants.MSG_ERR_GET_DATA);
			return resultMap;
		}

		double accountBalance = Double.parseDouble(card.getAccountBalance());
		double getMoney = Double.parseDouble(money);

		// 取款金额超过余额
		if (accountBalance < getMoney) {

			resultMap.put(Constants.RESULT_KEY_CODE, Constants.MSG_ERR_MAX_MONEY);
			return resultMap;
		}
		card.setAccountBalance(accountBalance - getMoney + "");

		Bill bill = new Bill();

		bill.setAffairType(Constants.CARD_OPERATE_GET_TYPE);
		bill.setCardID(card.getCardID());
		bill.setTradeBalance(money);
		DateFormatUtil dateFormatUtil = DateFormatUtil.getDateFormatUtil();
		bill.setTradeTime(dateFormatUtil.formatStrByDate(new Date()));
		List<Bill> bills = card.getBills();
		bills.add(bill);
		card.setBills(bills);

		// 保存银行卡账户数据
		String operateResult = dataOperateUtil.writeCardData(card);

		// 返回消息
		resultMap.put(Constants.RESULT_KEY_CODE, operateResult);
		// 操作成功返回卡对象
		if (operateResult.equals(Constants.MSG_SUCCESS)) {
			resultMap.put(Constants.RESULT_KEY_DATA, card);
		}
		return resultMap;
	}

	/* (非 Javadoc)
	 * 
	 * 
	 * @param cardID
	 * @param transferAccountID
	 * @param money
	 * @return
	 * @see com.atm.service.CardSvc#transferAccount(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Map<String, Object> transferAccount(String cardID, String transferAccountID, String money) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		DataOperateUtil dataOperateUtil = DataOperateUtil.getDataOperateUtil();
		Card card = dataOperateUtil.readCardData(cardID);

		if (card == null) {
			resultMap.put(Constants.RESULT_KEY_CODE, Constants.MSG_ERR_GET_DATA);
			return resultMap;
		}

		Card transferCard = dataOperateUtil.readCardData(transferAccountID);

		if (transferCard == null) {
			resultMap.put(Constants.RESULT_KEY_CODE,  Constants.MSG_ERR_TRANSFER_ACCOUNT_ID);
			return resultMap;
		}

		double accountBalance = Double.parseDouble(card.getAccountBalance());
		double getMoney = Double.parseDouble(money);

		// 取款金额超过余额
		if (accountBalance < getMoney) {

			resultMap.put(Constants.RESULT_KEY_CODE, Constants.MSG_ERR_MAX_MONEY);
			return resultMap;
		}
		card.setAccountBalance(accountBalance - getMoney + "");

		Bill bill = new Bill();

		bill.setAffairType(Constants.CARD_OPERATE_GET_TYPE);
		bill.setCardID(card.getCardID());
		bill.setTradeBalance(money);
		DateFormatUtil dateFormatUtil = DateFormatUtil.getDateFormatUtil();
		bill.setTradeTime(dateFormatUtil.formatStrByDate(new Date()));
		List<Bill> bills = card.getBills();
		bills.add(bill);
		card.setBills(bills);

		double transferCardBalance = Double.parseDouble(transferCard.getAccountBalance());
		double saveMoney = Double.parseDouble(money);
		transferCard.setAccountBalance(transferCardBalance + saveMoney + "");
		Bill transferCardBill = new Bill();

		transferCardBill.setAffairType(Constants.CARD_OPERATE_SAVE_TYPE);
		transferCardBill.setCardID(transferCard.getCardID());
		transferCardBill.setTradeBalance(money);
		transferCardBill.setTradeTime(dateFormatUtil.formatStrByDate(new Date()));
		List<Bill> transferCardBills = transferCard.getBills();
		transferCardBills.add(transferCardBill);
		transferCard.setBills(transferCardBills);

		// 保存银行卡账户数据
		String operateResult = dataOperateUtil.writeCardData(card);
		String transferResult = dataOperateUtil.writeCardData(transferCard);

		// 操作成功返回卡对象
		if (operateResult.equals(Constants.MSG_SUCCESS) && transferResult.equals(Constants.MSG_SUCCESS)) {
			resultMap.put(Constants.RESULT_KEY_CODE,Constants.MSG_SUCCESS);
			resultMap.put(Constants.RESULT_KEY_DATA, card);
		} else {
			// 返回消息
			resultMap.put(Constants.RESULT_KEY_CODE, Constants.MSG_ERR_SAVE_DATA);
		}
		return resultMap;
	}

	

}
